package com.chaodev.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.RoleDao;
import com.chaodev.base.dto.RoleDTO;
import com.chaodev.base.model.Power;
import com.chaodev.base.model.Role;
import com.chaodev.base.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleDao roleDao;


    @Override
    public PageInfo<RoleDTO> roleQuery(Map<String, Object> map) {
        int page = Integer.parseInt(map.get("P_PAGE").toString());
        int pageSize = Integer.parseInt(map.get("P_PAGESIZE").toString());
        PageHelper.startPage(page, pageSize);
        List<RoleDTO> roleList = roleDao.roleQuery(map);
        return new PageInfo<>(roleList, pageSize);
    }

    @Override
    public boolean roleAddOracle(Role role) {
        int result = roleDao.roleAddOracle(role);
        return result != 0;
    }

    @Override
    public boolean roleAddMysql(Role role) {
        int result = roleDao.roleAddMysql(role);
        return result != 0;
    }

    @Override
    public boolean roleUpd(Role role) {
        int result = roleDao.roleUpd(role);
        return result != 0;
    }

    @Override
    public boolean roleDelete(Role role) {
        int result = roleDao.roleDelete(role);
        return result != 0;
    }

    @Override
    public List<Map<String, Object>> getRoleTree(Map<String, Object> map) {
        return roleDao.getRoleTree(map);
    }

    @Override
    public List<Power> getPowerData(Map<String, Object> map) {
        return roleDao.getPowerData(map);
    }

    @Override
    public boolean powerAdd(Power power) {
        //先删除权限信息
        roleDao.powerDel(power);
        //新增权限信息
        int result = roleDao.powerAdd(power);
        return result != 0;
    }

    @Override
    public Role getRoleByRoleId(Integer roleId) {
        return roleDao.getRoleByRoleId(roleId);
    }

    @Override
    public boolean appPowerAdd(Power power) {
        //先删除权限信息
        roleDao.appPowerDel(power);
        //新增权限信息
        int result = roleDao.appPowerAdd(power);
        return result != 0;
    }

    @Override
    public List<Power> getAppPowerData(Map<String, Object> map) {
        return roleDao.getAppPowerData(map);
    }

}
